import logging
from django.core.exceptions import ObjectDoesNotExist
from drf_yasg.utils import swagger_auto_schema
from rest_framework.authentication import TokenAuthentication
from rest_framework.views import APIView
from api_test.common.api_response import JsonResponse
from api_test.models import GameInfo
from api_test.serializers import GameSerializer

logger = logging.getLogger(__name__)  # 这里使用 __name__ 动态搜索定义的 logger 配置，这里有一个层次关系的知识点。


class GameList(APIView):
    authentication_classes = (TokenAuthentication,)
    permission_classes = ()

    @swagger_auto_schema(manual_parameters=[])
    def get(self, request):
        """
        获取游戏列表
        """
        try:
            game_obj = GameInfo.objects.all()
        except ObjectDoesNotExist:
            return JsonResponse(code="999995", msg="游戏列表不存在!")

        serialize = GameSerializer(game_obj, many=True)
        return JsonResponse(data={"gameList": serialize.data}, code="999999", msg="成功!")
